package com.dtsheep.easyexcel.service.local;

import com.alibaba.excel.EasyExcel;
import com.dtsheep.easyexcel.po.ExpressCompany;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * InputLocal
 *
 * <p>
 *     本地文件导入
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/26
 */
@Component
public class InputLocalService {

    /**
     * 快递100 快递公司编码
     */
    private final static String KUAIDI_COMPANY_FILE_PATH = "/Users/klys/project/java/easyexcel-spring-boot-starter/src/main/java/com/dtsheep/easyexcel/file/快递100快递公司标准编码.xlsx";

    /**
     * 从本地文件读取
     *
     * @param filePath 文件路径
     */
    public static List<ExpressCompany> getByFile(String filePath){
        // 从本地文件读取
        File file = new File(filePath);
        List<ExpressCompany> objectList = EasyExcel
                .read(file)
                .sheet(0)
                .headRowNumber(2)
                .head(ExpressCompany.class)
                .doReadSync();
        return objectList;
    }

    /**
     * 从输入流读取
     */
    private static List<ExpressCompany> getByInputStream(String filePath) throws FileNotFoundException {
        InputStream file = new FileInputStream(filePath);
        List<ExpressCompany> objectList = EasyExcel
                .read(file)
                .sheet(0)
                .headRowNumber(2)
                .head(ExpressCompany.class)
                .doReadSync();
        return objectList;
    }

    public static void main(String[] args) {
        List<ExpressCompany> byFile = getByFile(KUAIDI_COMPANY_FILE_PATH);
        System.out.println(byFile);
    }



}
